package cn.yangliu.jdbc.config;

import cn.yangliu.jdbc.dymanicdb.DataSourceEnum;
import cn.yangliu.jdbc.dymanicdb.DynamicDataSourceResolver;
import cn.yangliu.comm.BaseComponent;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
import net.sf.log4jdbc.Log4jdbcProxyDataSource;
import org.springframework.aop.Advisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者 杨柳
 * 创建时间 2017-08-24 01:44
 */
@Configuration
public class DataSourceConfig extends BaseComponent{

    @Bean
    public Log4jdbcProxyDataSource defaultDataSourceLog(){
        return new Log4jdbcProxyDataSource(defaultDataSource());
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DruidDataSource defaultDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "dataSource")
    public DataSource dataSource() {
        logger.info("=================>dataSource");
        DynamicDataSourceResolver resolver = new DynamicDataSourceResolver();
        Map<Object, Object> dataSources = new HashMap<>();
        resolver.setTargetDataSources(dataSources);

        DataSource defaultDataSource = defaultDataSourceLog();
        dataSources.put(DataSourceEnum.DEFAULT.getTarget(), defaultDataSourceLog());


        /*--------默认数据源--------*/
        resolver.setDefaultTargetDataSource(defaultDataSource);
        return resolver;
    }


    @Bean
    public ServletRegistrationBean indexServletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new StatViewServlet());
        registration.addUrlMappings("/durid/*");
        registration.setName("duridStatViewServlet");
        Map<String,String> params = new HashMap<>();
        params.put("loginUsername","durid");
        params.put("loginPassword","durid");
        params.put("resetEnable","false");// 禁用HTML页面上的“Reset All”功能
//        params.put("allow","127.0.0.1");/ IP白名单 (没有配置或者为空，则允许所有访问)
        registration.setInitParameters(params);
        return registration;
    }

    @Bean
    public FilterRegistrationBean indexFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean(new WebStatFilter());
        registration.addUrlPatterns("/*");
        registration.setName("druidWebStatFilter");
        Map<String,String> params = new HashMap<>();
        params.put("profileEnable","true");
        params.put("principalCookieName","USER_COOKIE");
        params.put("principalSessionName","USER_SESSION");
        params.put("exclusions","*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
        registration.setInitParameters(params);
        return registration;
    }
    @Bean
    public DruidStatInterceptor druidStatInterceptor(){
        return new DruidStatInterceptor();
    }
    @Bean
    public JdkRegexpMethodPointcut druidStatPointcut(){
        JdkRegexpMethodPointcut druidStatPointcut = new JdkRegexpMethodPointcut();
        String service_patterns = "cn.yangliu.service.*.*";
        String mapper_patterns = "cn.yangliu.mybatis.mapper.*";
        String repository_patterns = "cn.yangliu.jpa.repository.*";
        druidStatPointcut.setPatterns(service_patterns,mapper_patterns,repository_patterns);
        return druidStatPointcut;
    }
    @Bean
    public Advisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor,JdkRegexpMethodPointcut druidStatPointcut) {
        return new DefaultPointcutAdvisor(druidStatPointcut, druidStatInterceptor);
    }

}
